// loginpackage/pages/register/register.js
const app = getApp()
import Notify from '@vant/weapp/notify/notify';
import Dialog from '@vant/weapp/dialog/dialog';

Page({

  /**
   * 页面的初始数据
   */
  data: {
    accountErrorMsg: '',
    pwdOneErrorMsg: '',
    pwdTwoErrorMsg: '',
    account: '',
    onePassword: '',
    twoPassword: '',
    url: app.globalData.baseUrl
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {

  },

  //获取账号输入框的值
  accountChange(e) {
    this.setData({
      account: e.detail
    })
  },
  //获取第一次密码的值
  pwdOneChange(e) {
    this.setData({
      onePassword: e.detail
    })
  },
  //获取第二次密码的值
  pwdTwoChange(e) {
    this.setData({
      twoPassword: e.detail
    })
  },
  //点击注册按钮
  registerCilck() {
    //判断输入的值
    const account = this.data.account
    const onePwd = this.data.onePassword
    const twoPwd = this.data.twoPassword
    //判断账号
    if (account === null || account === '') {
      this.setData({
        accountErrorMsg: '请输入账号'
      })
      return
    } else if (account.length < 5 || account.length > 30) {
      this.setData({
        accountErrorMsg: '注册的账号长度在5到30之间'
      })
      return
    } else {
      this.setData({
        accountErrorMsg: ''
      })
    }
    //判断密码
    if (onePwd === null || onePwd === '') {
      this.setData({
        pwdOneErrorMsg: '请输入密码'
      })
      return
    } else if (onePwd.length < 10 || onePwd.length > 30) {
      this.setData({
        pwdOneErrorMsg: '密码长度在10到30之间'
      })
      return
    } else {
      this.setData({
        pwdOneErrorMsg: ''
      })
    }
    //判断确认密码
    if (twoPwd === null || twoPwd === '') {
      this.setData({
        pwdTwoErrorMsg: '请输入密码'
      })
      return
    } else if (twoPwd.length < 10 || twoPwd.length > 30) {
      this.setData({
        pwdTwoErrorMsg: '密码长度在10到30之间'
      })
      return
    } else {
      this.setData({
        pwdTwoErrorMsg: ''
      })
    }
    //判断两次输入的密码是否一致
    if (onePwd !== twoPwd) {
      this.setData({
        pwdOneErrorMsg: '两次输入的密码不一致',
        pwdTwoErrorMsg: '两次输入的密码不一致'
      })
      return
    } else {
      this.setData({
        pwdOneErrorMsg: '',
        pwdTwoErrorMsg: ''
      })
    }
    wx.showLoading({
      title: '正在注册'
    })
    //存储注册信息
    // wx.setStorageSync('register_account', this.data.account)
    //发送登录请求
    this.registerRequest()
  },

  /**
   * 注册请求
   * */
  registerRequest() {
    wx.request({
      url: this.data.url + "/user/userRegister",
      method: 'POST',
      header: {
        'content-type': 'application/x-www-form-urlencoded'
      },
      data: {
        account: this.data.account,
        onePwd: this.data.onePassword,
        twoPwd: this.data.twoPassword
      },
      success: function (res) {
        wx.hideLoading()
        const msg = res.data.msg
        if (msg !== '注册成功') {
          Notify({ type: 'warning', message: msg });
          return
        }
        Dialog.alert({
          title: '提示',
          message: '注册成功，去登录!',
        }).then(() => {
          //返回登录页
          wx.navigateBack()
        });
      },
      fail: function () {
        wx.hideLoading()
        wx.showToast({
          title: '注册失败',
          icon: 'error'
        })
      }
    })
  }
})